package org.example.managementsystem.service;

import org.example.managementsystem.model.DeviceScrap;
import org.example.managementsystem.model.DeviceScrapReview;
import org.example.managementsystem.repository.DeviceScrapDao;
import org.example.managementsystem.repository.DeviceScrapReviewDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 设备报废审核服务层
 */
@Service
public class DeviceScrapReviewService {

    private final DeviceScrapReviewDao deviceScrapReviewDao;
    private final DeviceScrapDao deviceScrapDao;

    @Autowired
    public DeviceScrapReviewService(DeviceScrapReviewDao deviceScrapReviewDao, DeviceScrapDao deviceScrapDao) {
        this.deviceScrapReviewDao = deviceScrapReviewDao;
        this.deviceScrapDao = deviceScrapDao;
    }

    /**
     * 审核设备报废申请
     */
    public void reviewScrap(int scrapId, boolean isApproved, String description, String reviewerJobNumber) {

        // 先查询表中是否有对应id的审核记录
        DeviceScrapReview existingReview = deviceScrapReviewDao.getReviewByScrapId(scrapId);
        if(existingReview != null){
            // 如果有，则更新该记录
            existingReview.setIsApproved(isApproved);
            existingReview.setDescription(description);
            existingReview.setReviewerJobNumber(reviewerJobNumber);
            existingReview.setReviewTime(LocalDateTime.now());
            deviceScrapReviewDao.updateReview(existingReview);

            // 报废审核更新成功后，更新报废表中的审核状态
            deviceScrapDao.updateScrapStatus(scrapId, isApproved);
        }
        else{
            DeviceScrapReview review = new DeviceScrapReview();
            review.setScrapId(scrapId);
            review.setIsApproved(isApproved);
            review.setDescription(description);
            review.setReviewerJobNumber(reviewerJobNumber);
            review.setReviewTime(LocalDateTime.now());
            deviceScrapReviewDao.insertReview(review);

            // 报废审核更新成功后，更新报废表中的审核状态
            deviceScrapDao.updateScrapStatus(scrapId, isApproved);
        }
    }

    public void reviewScrap(DeviceScrapReview review) {

        // 先查询表中是否有对应id的审核记录
        DeviceScrapReview existingReview = deviceScrapReviewDao.getReviewByScrapId(review.getScrapId());

        if(existingReview != null){
            // 如果有，则更新该记录
            existingReview.setIsApproved(review.getIsApproved());
            existingReview.setDescription(review.getDescription());
            existingReview.setReviewerJobNumber(review.getReviewerJobNumber());
            existingReview.setReviewTime(LocalDateTime.now());
            deviceScrapReviewDao.updateReview(existingReview);

            // 报废审核更新成功后，更新报废表中的审核状态
            deviceScrapDao.updateScrapStatus(review.getScrapId(), review.getIsApproved(), true);
        }
        else {
            // 设置审核时间
            review.setReviewTime(LocalDateTime.now());
            deviceScrapReviewDao.insertReview(review);

            // 报废审核更新成功后，更新报废表中的审核状态
            deviceScrapDao.updateScrapStatus(review.getScrapId(), review.getIsApproved(), true);
        }
    }

    /**
     * 更新报废审核记录
     */
    public void updateReview(DeviceScrapReview review) {
        deviceScrapReviewDao.updateReview(review);
    }

    /**
     * 获取所有历史报废审核记录
     */
    public List<DeviceScrapReview> getAllReviews() {
        return deviceScrapReviewDao.getAllReviews();
    }

    /**
     * 根据ID获取报废审核记录
     */
    public DeviceScrapReview getReviewById(int scrapId) {
        return deviceScrapReviewDao.getReviewByScrapId(scrapId);
    }
}